package org.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.Category;
import org.csu.mypetstore.api.entity.Item;
import org.csu.mypetstore.api.entity.ItemInventory;
import org.csu.mypetstore.api.entity.Product;
import org.csu.mypetstore.api.persistence.CategoryMapper;
import org.csu.mypetstore.api.persistence.ItemInventoryMapper;
import org.csu.mypetstore.api.persistence.ItemMapper;
import org.csu.mypetstore.api.persistence.ProductMapper;
import org.csu.mypetstore.api.service.CatalogService;
import org.csu.mypetstore.api.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ItemInventoryMapper itemInventoryMapper;

    //***************商品展示模块***************//
    //获取所有categories
    @Override
    public CommonResponse<List<Category>> getCategoryList() {
        List<Category> categoryList = categoryMapper.selectList(null);
        try {
        //不会返回null，返回长度为0
        if(categoryList.isEmpty()){
            return CommonResponse.createForSuccessMessage("没有分类信息");
        }
        return CommonResponse.createForSuccess(categoryList);
        } catch (Exception e) {
            System.out.println("categories查询出错"+ e);
            return CommonResponse.createForError("categories查询出错");
        }
    }

    //由categories的id获取单个categories
    @Override
    public CommonResponse<Category> getCategoryByCategoryId(String categoryId) {
        try {
            Category category = categoryMapper.selectById(categoryId);
            if (category == null) {
                return CommonResponse.createForSuccessMessage("没有该ID的category信息");
            }
            return CommonResponse.createForSuccess(category);
        } catch (Exception e) {
            System.out.println("categories查询出错"+ e);
            return CommonResponse.createForError("categories查询出错");
        }
    }

    //由categories的id获取其下所有product
    @Override
    public CommonResponse<List<Product>> getProductListByCategoryId(String categoryId) {
        try {
            QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("category",categoryId);
            List<Product> productList = productMapper.selectList(queryWrapper);
            if(productList.isEmpty()){
                return CommonResponse.createForSuccessMessage("该分类下没有Product子分类");
            }
            return CommonResponse.createForSuccess(productList);
        } catch (Exception e) {
            System.out.println("products查询出错"+ e);
            return CommonResponse.createForError("products查询出错");
        }
    }

    //获取所有product
    @Override
    public CommonResponse<List<Product>> getProduct() {
        try {
            List<Product> products = productMapper.selectList(null);
            if (products.isEmpty()) {
                return CommonResponse.createForSuccessMessage("没有product产品信息");
            }
            return CommonResponse.createForSuccess(products);
        } catch (Exception e) {
            System.out.println("products查询出错"+ e);
            return CommonResponse.createForError("products查询出错");
        }
    }

    //由product的id获取单个product
    @Override
    public CommonResponse<Product> getProductByProductId(String productId) {
        try {
            Product product = productMapper.selectById(productId);
            if(product == null){
                return CommonResponse.createForSuccessMessage("没有该Product的信息");
            }
            return CommonResponse.createForSuccess(product);
        } catch (Exception e) {
            System.out.println("products查询出错"+ e);
            return CommonResponse.createForError("products查询出错");
        }
    }

    //由product的id获取其下所有item
    @Override
    public CommonResponse<List<ItemVO>> getItemsByProductId(String productId) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productId);
        try {
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if(itemList.isEmpty()){
            return CommonResponse.createForSuccessMessage("该product下没有item商品");
        }

        Product product = productMapper.selectById(productId);

        List<ItemVO> itemVOList = new ArrayList<>();
        for(Item item : itemList){
            ItemVO itemVO = itemToItemVO(item,product);
            itemVOList.add(itemVO);
        }
        return CommonResponse.createForSuccess(itemVOList);
        } catch (Exception e) {
            System.out.println("items查询出错"+ e);
            return CommonResponse.createForError("items查询出错");
        }
    }

    //由item的id获取单个item
    @Override
    public CommonResponse<ItemVO> getItemByItemId(String itemId) {
        try {
            Item item = itemMapper.selectById(itemId);
            if (item == null) {
                return CommonResponse.createForSuccessMessage("该item不存在");
            }

            Product product = productMapper.selectById(item.getProductId());
            if (product == null) {
                return CommonResponse.createForSuccessMessage("该item下的product信息不存在");
            }
            ItemVO itemVO = this.itemToItemVO(item, product);
            return CommonResponse.createForSuccess(itemVO);
        }catch (Exception e) {
            System.out.println("items查询出错"+ e);
            return CommonResponse.createForError("items查询出错");
        }
    }

    //获取所有item
    @Override
    public CommonResponse<List<ItemVO>> getItems() {
        try {
            List<Product> products = productMapper.selectList(null);
            if (products.isEmpty()) {
                return CommonResponse.createForSuccessMessage("item商品查询无结果(product)");
            }
            // 查询结果放入HashMap方便之后的查找
            Map<String, Product> proMap = new HashMap<>();
            for (Product product : products) {
                proMap.put(product.getProductId(), product);
            }

            List<Item> items = itemMapper.selectList(null);
            if (items.isEmpty()) {
                return CommonResponse.createForSuccessMessage("item商品查询无结果(item)");
            }

            List<ItemVO> itemVOList = new ArrayList<>();
            for (Item item : items) {
                Product product = proMap.get(item.getProductId());
                ItemVO itemVO = itemToItemVO(item, product);
                itemVOList.add(itemVO);
            }
            return CommonResponse.createForSuccess(itemVOList);
        }catch (Exception e) {
            System.out.println("items查询出错"+ e);
            return CommonResponse.createForError("items查询出错");
        }
    }

    //由item和product获取itemVO
    private ItemVO itemToItemVO(Item item,Product product){
        ItemVO itemVO = new ItemVO();
        itemVO.setItemId(item.getItemId());
        itemVO.setProductId(item.getProductId());
        itemVO.setListPrice(item.getListPrice());
        itemVO.setUnitCost(item.getUnitCost());
        itemVO.setSupplierId(item.getSupplierId());
        itemVO.setStatus(item.getStatus());
        itemVO.setAttribute1(itemVO.getAttribute1());
        itemVO.setAttribute2(itemVO.getAttribute2());
        itemVO.setAttribute3(itemVO.getAttribute3());
        itemVO.setAttribute4(itemVO.getAttribute4());
        itemVO.setAttribute5(itemVO.getAttribute5());

        itemVO.setCategoryId(product.getCategoryId());
        itemVO.setProductName(product.getName());
        itemVO.setProductDescription(product.getDescription());
        itemVO.setPicture(product.getPicture());

        ItemInventory itemInventory = itemInventoryMapper.selectById(item.getItemId());
        itemVO.setQuantity(itemInventory.getQuantity());

        return itemVO;
    }

    //***************商品搜索***************//
    //搜索product

   @Override
    public CommonResponse<List<Product>> getProductByName(String name) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        List<Product> productList = productMapper.selectList(queryWrapper);
        return CommonResponse.createForSuccess(productList);
    }

}
